<?php

namespace app\api\controller;

use app\Response;
use app\BaseController;
use app\api\service\LoginService;
use app\api\service\WechatService;
use think\facade\Request;

class Login extends BaseController
{
    public function login(){
        if (Request::isPost()) {
            $data = $this->request->post();
            try{
                $this->validate($data, 'UserLogin');
            }catch (\Exception $e){
                return Response::wrapError(0,$e->getMessage());
            }
            $result = LoginService::loginService($data);
            return $result;
        }
    }


    /** 微信授权登录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function wxLogin(){
        $code = Request::post('code');
        $data = [
            'code'=>$code,
        ];
        try{
            $this->validate($data, 'WxLogin');
        }catch (\Exception $e){
            return Response::wrapError($e->getMessage());
        }

        $info = WechatService::wxLogin($code);
        return $info;
    }


    public function wxRegister(){
        $code = Request::post('code');
        $openid = Request::post('openid');
        $extension = $this->request->post('extension',"");
        $info = WechatService::wxRegisterService($code,$openid,$extension);
        return $info;
    }
}